﻿using ApiManage.Models.RequestModels;
using ApiManage.Svc.Filter;
using Microsoft.AspNetCore.Mvc;
using MongoDB.Bson;
using MongoDB.Driver;
using Rhonin.Common.Entity.Api;
using Rhonin.Common.Entity.Exception;
using Rhonin.Common.Entity.Page;
using Rhonin.Common.Extentions;
using System;
using System.Linq;
using System.Linq.Expressions;
using ApiManage.Models.MongoMappings.Project;
using ApiManage.Applications.Mongo.Project;
using ApiManage.Service.Mongo.Project;

namespace ApiManage.Svc.Controllers.Project
{
    /// <inheritdoc />
    /// <summary>
    /// 模块列表
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class ModuleController : BaseController
    {
        private readonly IModuleService _moduleService;
        private readonly IModulesQueryService _moduleQueryService;
        /// <summary>
        /// 
        /// </summary>
        public ModuleController(IModuleService moduleService,
            IModulesQueryService moduleQueryService)
        {
            _moduleService = moduleService;
            _moduleQueryService = moduleQueryService;
        }
        /// <summary>
        /// 分页获取
        /// </summary>
        /// <param name="name"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [Route("GetPage"), HttpGet, CheckLogin]
        public ApiResult<PageResult<Modules>> GetPage(string name, int pageIndex, int pageSize)
        {
            var param = new QueryPage
            {
                PageIndex = pageIndex,
                PageSize = pageSize
            };
            Expression<Func<Modules, bool>> where = o => o.Status != "Delete";
            where = where.AndIf(o => o.Name.Contains(name), name.IsNotEmpty());
            var list = _moduleQueryService.GetList(where);
            var page = new PageResult<Modules>
            {
                TotalCount = list.Count,
                Items = list.OrderBy(o => o.Status).ThenByDescending(o => o.CreateTime).Skip(param.Skip).Take(param.Limit),
                PageParameter = param
            };
            return CreateResult(page);
        }
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        [Route("Save"), HttpPost, CheckLogin]
        public ApiResult Save(ModulesExtend param)
        {
            if (string.IsNullOrEmpty(param.Name))
                throw new ApiException("名称不能为空");
            if (string.IsNullOrEmpty(param.ProjectId))
                throw new ApiException("所属项目不能为空");
            if (param.Id.IsNullOrEmpty())
            {
                _moduleService.Insert(new Modules {
                    Name = param.Name,
                    Description = param.Description,
                    Sort = param.Sort,
                    Path = param.Path,
                    Status = param.Status,
                    ProjectId = param.ProjectId,
                    CreateUser = UserId
                });
            }
            else
            {
                _moduleService.Update(u => u.Id == new ObjectId(param.Id), Builders<Modules>.Update
                    .Set("Name", param.Name)
                    .Set("Description", param.Description)
                    .Set("Path", param.Path)
                    .Set("Sort", param.Sort)
                    .Set("Status", param.Status)
                    .Set("ProjectId", param.ProjectId));
            }
            return CreateSuccess();
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        [Route("Remove"), HttpPost, CheckLogin]
        public ApiResult Remove(IdParam param)
        {
            try
            {
                _moduleService.Delete(u => u.Id == new ObjectId(param.Id));
            }
            catch (Exception ex)
            {
                return CreateError(ex.Message);
            }
            return CreateSuccess();
        }
    }
}